<?php
if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}
include "safe.class.php";
$safe = new safe_weitao_plugin_online_alipay();
$tid = $safe->getparameter('tid',1,1);
$credit = $_G['cache']['plugin']['online_alipay']['count'];
$manage = $_G['cache']['plugin']['online_alipay']['manage'];
$manage = unserialize($manage);
$count = DB::table("common_member_count");
$common_member = DB::table("common_member");
$thread_guarantee =  DB::table("thread_guarantee");
$thread_alipayorder =  DB::table("thread_alipayorder");
$paramaterstatus = $safe->getparameter('status',1,0);
if(empty($tid)) {
	showmessage("未定义的操作!");
}

if(!empty($paramaterstatus)) {
	if($paramaterstatus == 'freeze') {
		if(!in_array($_G['groupid'],$manage)) {
			showmessage("无权限操作");
		}
		$query = DB::query("Update `$thread_guarantee` SET `status` = '5' WHERE `fid`='$tid';");
		if($query) {
			showmessage("交易冻结成功",'plugin.php?id=online_alipay:index');
		} else {
			showmessage("交易冻结失败");
		}
	} else if($paramaterstatus == 'unfreeze') {
		if(!in_array($_G['groupid'],$manage)) {
			showmessage("无权限操作");
		}
		$query = DB::query("Update `$thread_guarantee` SET `status` = '1' WHERE `fid`='$tid';");
		if($query) {
			showmessage("交易解冻成功",'plugin.php?id=online_alipay:index');
		} else {
			showmessage("交易解冻失败");
		}
	}
}

$query = DB::query("SELECT * FROM `$thread_guarantee` WHERE `fid`='$tid'");
if(!mysql_fetch_array($query)) {
	showmessage("找不到该交易");
}
$thread_guarantee = DB::table("thread_guarantee");
$query = DB::query("SELECT * FROM `$thread_guarantee` WHERE `fid`='$tid'");
while($list = mysql_fetch_array($query)) {
	$post = $list[6];
	$sellername = $list[2];
	$buyername = $list[3];
	$status = $list[10];
}
$query = DB::query("SELECT * FROM `$common_member` WHERE `username` = '$sellername' or `username` = '$buyername'");
while($fetch = mysql_fetch_array($query)) {
	if($fetch['username'] == $buyername) {
		$buyerid = $fetch['uid'];
	}
	if($fetch['username'] == $sellername) {
		$sellerid = $fetch['uid'];
	}
}

if($status == 0) {
	if($post == 0) { //seller
		if($_G['username'] != $buyername) {
			showmessage("请等待买家确认!");
		}
	} else { // buyer
		if($_G['username'] != $sellername) {
			showmessage("请等待卖家确认!");
		}
	}
	$query = DB::query("Update `$thread_guarantee` SET `status` = '1' WHERE `fid`='$tid';");
	notification_add($sellerid ,'post', "<a href='forum.php?mod=viewthread&tid={tid}'>交易双方已达成交易，请等待管理员审核，点击这里查看交易</a>" ,array('tid' => $tid),$system = 1);
	notification_add($buerid ,'post', "<a href='forum.php?mod=viewthread&tid={tid}'>交易双方已达成交易，请等待管理员审核，点击这里查看交易</a>" ,array('tid' => $tid),$system = 1);
	if($query) {
		showmessage("订单状态更新成功!",'plugin.php?id=online_alipay:index');
	} else {
		showmessage("订单状态修改失败!");
	}

} else if ($status  == 1) {
	if(!in_array($_G['groupid'],$manage)) {
		showmessage("请等待管理员审核!");
	}
	notification_add($sellerid ,'post', "<a href='forum.php?mod=viewthread&tid={tid}'>管理员审核成功，请卖家及时发货完成交易!</a>" ,array('tid' => $tid),$system = 1);
	notification_add($buerid ,'post', "<a href='forum.php?mod=viewthread&tid={tid}'>管理员审核成功，请卖家及时发货完成交易</a>" ,array('tid' => $tid),$system = 1);

	$query = DB::query("Update `$thread_guarantee` SET `status` = '2' WHERE `fid`='$tid';");
	if($query) {
		showmessage("订单状态更新成功!",'plugin.php?id=online_alipay:index');
	} else {
		showmessage("订单状态修改失败!");
	}

} else if($status == 2) {
	if($_G['username'] != $sellername) {
		showmessage("请等待确认完成任务!");
	}
	notification_add($sellerid ,'post', "<a href='forum.php?mod=viewthread&tid={tid}'>卖家已完成请等待买家确认!</a>" ,array('tid' => $tid),$system = 1);
	notification_add($buerid ,'post', "<a href='forum.php?mod=viewthread&tid={tid}'>卖家已完成请等待买家确认</a>" ,array('tid' => $tid),$system = 1);

	$query = DB::query("Update `$thread_guarantee` SET `status` = '3' WHERE `fid`='$tid';");
	if($query) {
		showmessage("订单状态更新成功!",'plugin.php?id=online_alipay:index');
	} else {
		showmessage("订单状态修改失败!");
	}
} else if($status == 3) {
	if($_G['username'] != $buyername) {
		showmessage("请等待买家验收工作!");
	}
	notification_add($sellerid ,'post', "<a href='forum.php?mod=viewthread&tid={tid}'>买家已确认收货交易完成!</a>" ,array('tid' => $tid),$system = 1);
	notification_add($buerid ,'post', "<a href='forum.php?mod=viewthread&tid={tid}'>买家已确认收货交易完成</a>" ,array('tid' => $tid),$system = 1);

	$query = DB::query("SELECT * FROM `$thread_guarantee` WHERE `fid`='$tid'");
	while($typequery = mysql_fetch_array($query)) {
		if($typequery['threadtype'] == 1) {
			$price = $typequery['seller'];
			$common_member = DB::table("common_member");
			$query = DB::query("SELECT *FROM `$common_member` WHERE `username` = '".$typequery['sellername']."'");
			while($getid = mysql_fetch_array($query)) {$my_seller_uid = $getid[0];}
			updatemembercount($my_seller_uid,array("extcredits$credit" => "$price"));
		} else if($typequery['threadtype'] == 2) {
			$query = DB::query("Update `$thread_alipayorder` SET `status` = '3' WHERE `threadid`='$tid'");
		}
	}
	$query = DB::query("Update `$thread_guarantee` SET `status` = '4' WHERE `fid`='$tid'");
	if($query) {
		showmessage("订单状态更新成功!",'plugin.php?id=online_alipay:index');
	} else {
		showmessage("订单状态修改失败!");
	}
} else if($status == 5) {
	if(!in_array($_G['groupid'],$manage)) {
		showmessage("请等待管理员审核!");
	}
	$query = DB::query("Update `$thread_guarantee` SET `status` = '5' WHERE `fid`='$tid';");
	if($query) {
		showmessage("订单状态更新成功!",'plugin.php?id=online_alipay:index');
	} else {
		showmessage("订单状态修改失败!");
	}
} else {
	showmessage("订单已完成，或订单状态错误!");
}
?>